package com.qf.web.controller;/**
 * @author Administrator
 * @version v1.0
 * @date 2022/4/21
 */

import com.qf.domain.GoodsType;
import com.qf.domain.GtPageBean;
import com.qf.service.GoodsTypeService;
import com.qf.service.impl.GoodsTypeServiceImpl;
import com.qf.util.StringUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * projectName:Project_2202
 * author:
 * time:2022/4/21 13:51
 * description:
 */
@WebServlet("/getGoodsType")
public class GetGoodsTypeServlet extends BaseServlet {
    GoodsTypeService goodsTypeService = new GoodsTypeServiceImpl();
    public String show(HttpServletRequest request, HttpServletResponse response)throws IOException {

//        List<GoodsType> goodsTypes =  goodsTypeService.findALL();
//        if(goodsTypes!=null){
//            request.setAttribute("goodsTypeList", goodsTypes);
//            return "forward:/admin/showGoodsType.jsp";
//        }
//       return "redirect:/admin/admin.jsp";
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        String pageNum = request.getParameter("pageNum");
        String pageSize = request.getParameter("pageSize");
        int page_num = 1;
        int page_size = 8;

            if(!StringUtils.isEmpty(pageNum)){
                page_num = Integer.parseInt(pageNum);
                if(page_num<1){
                    page_num=1;
                }
                System.out.println(page_num);
            }
            if(!StringUtils.isEmpty(pageSize)){
                page_size = Integer.parseInt(pageSize);
                if(page_size<1){
                    page_size=8;
                }
            }
           GtPageBean<GoodsType> gtPageBean =  goodsTypeService.findAllByPage(page_num,page_size);
            if(gtPageBean!=null){
                request.setAttribute("gtpageBean", gtPageBean);
                return "forward:/admin/showGoodsType.jsp";
            }
            return "redirect:/admin/admin.jsp";
    }

    public String update(HttpServletRequest request, HttpServletResponse response)throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        String id = request.getParameter("id");
        if(StringUtils.isEmpty(id)){
            return "forward:/admin/showGoodsType.jsp";
        }
        GoodsType goodsType = goodsTypeService.findById(Integer.parseInt(id));
        if(goodsType!=null){
            request.setAttribute("goodsType", goodsType);
            return "forward:/admin/UpdateGoodsType.jsp";
        }else{
            return "forward:/admin/showGoodsType.jsp";
        }
    }

    public String updateI(HttpServletRequest request, HttpServletResponse response)throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        if(StringUtils.isEmpty(name)){
            return "forward:/admin/UpdateGoodsType.jsp";
        }
        GoodsType goodsType = new GoodsType();
        goodsType.setId(Integer.parseInt(id));
        goodsType.setName(name);
        goodsTypeService.update(goodsType);
        return "redirect:/getGoodsType?method=show";
    }
    public String add(HttpServletRequest request, HttpServletResponse response)throws IOException {
        List<GoodsType> all = goodsTypeService.findALL();
        if(all!=null){
            request.setAttribute("goodsTypeList", all);
            return "forward:/admin/addGoodsType.jsp";
        }
        return "redirect:/getGoodsType?method=show";
    }
}